<?php
/**
 * Created by PhpStorm MK-AC-1010-Mcake会员大调查
 * User: allen.qiang
 * Date: 2015-04-16
 * Email: allen.qiang@fistforward.com
 */
quanli_require('module/base/activity/activityConfig.class.php');
quanli_require('module/base/customer/quanliCust.class.php');
//加载会员促销类
quanli_include('module/base/promotion/promotionCustomer.class.php');

class activityMemberSurvey {

    public $configName = "member_survey";//activities_extend表活动编号

    /**
     * 问卷信息收集
     * $mobile 手机号
     * $array 问卷答题信息数组
     * @author  2015-04-16 allen.qiang@fistforward.com
     */
    public function get_activities_extend($mobile,$array) {

        global $G_SHOP;

        try{
            $G_SHOP->DBCA->beginTransaction();

            $sql = "SELECT COUNT(uuid) AS num FROM `activities_extend` WHERE activityNumber='{$this->configName}' AND creator='{$mobile}'";

            $result = $G_SHOP->DBCA->stmt_query($sql);

            if($result && $result[0]->num>0){
                $G_SHOP->DBCA->commit();
                return array( 'code' => 500, 'message' => '该手机号已提交过问卷<br>感谢您的参与！' );
            }else{
                $txt = array(
                    1=>'很满意',
                    2=>'满意',
                    3=>'一般',
                    4=>'不满意',
                    5=>'很不满意'
                );
                foreach($array as $val)
                {
                    //保存答卷内容
                    $activities_extend = new entity('activities_extend');
                    $activities_extend->creator = $mobile; //创建人
                    $activities_extend->activityNumber = $this->configName; //活动编号
                    $activities_extend->classifyID = 'log'; //日志类型
                    $activities_extend->extend_4 = $txt[$val['appraise'][0]]; //第1_1题答案
                    $activities_extend->extend_5 = $txt[$val['appraise'][1]]; //第1_2题答案
                    $activities_extend->extend_6 = $txt[$val['appraise'][2]]; //第1_3题答案
                    $activities_extend->extend_7 = $val['recommend']; //第2题答案
                    $activities_extend->extend_8 = $val["suggest"]; //第3题答案

                    if (!$G_SHOP->DBCA->save($activities_extend)) {
                        throw new Exception('页面已过期，请刷新页面重试', 404);
                    }
                }

                $G_SHOP->DBCA->commit();
                return array( 'code' => 200, 'message' => '您的答卷已提交，感谢您的参与！' );
            }

        } catch (Exception $e) {

            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 700, 'message' => '系统错误' );
            }
        }
    }

}